import { _decorator, Label, Node } from 'cc';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { GamePPSuccess_Ctr } from '../../GamePlay/GameZt/GamePPSuccess_Ctr';
import { Respones } from '../../Config/GameCfg';
import { GameTip } from '../../../../Framework/Scripts/Managers/TipMgr';
import { ThemeMatchPPRes, ThemeMatchPPStopRes } from '../../../AssetPackage/Props/Hall/Game/GameZT';
const { ccclass, property } = _decorator;

@ccclass('GamePP_Ctr')
export class GamePP_Ctr extends UIComponent {
    dian: Node;
    isClick: Boolean;
    isClick1: boolean;

    //主题赛类型
    dwType: number; 
    labTime: any;
    timeCur: number;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.labTime = this.ViewComponent('bg/labTime',Label);
        this.dian = this.ViewNode('bg/btnTop/spDian1');
        this.AddButtonListener('btnBack',this,this.OnBtnClosClick1);
        this.AddButtonListener('bg/btnTop',this,this.OnBtnStartPPClick);
        this.AddButtonListener('bg/btnStop',this,this.OnBtnStopPPClick);
    }

    private setTime(){
        this.timeCur++;
        this.labTime.string = `${this.timeCur}S`;
    }
    private InitData(){
        this.dian.active = false;
        this.isClick = true;
        this.isClick1 = true;

        this.setBgAni(0);
    }

    private setWaitTime(){
        this.timeCur = 0;
        this.schedule(this.setTime,1)
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchPPRes,this.onmsgThemeMatchPPRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchPPStopRes,this.onmsgThemeMatchPPStopRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchPPRes,this.onmsgThemeMatchPPRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchPPStopRes,this.onmsgThemeMatchPPStopRes,this);
    }

    private OnBtnClosClick1(){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameStart1/GameStart1',UIType.Full);
    }

    //开始匹配
    private OnBtnStartPPClick(){
        if(this.isClick === false){
            return;
        }
        this.isClick = false;
        this.PlayAni(true);

        console.log('========OnBtnStartPPClick===========',this.dwType);
        globalThis.gameMgr._GameHallMsg.ThemeMatchPPReq(this.dwType);

        this.setWaitTime();
    }

    //停止匹配
    private OnBtnStopPPClick(){
        if(this.isClick1 === false){
            return;
        }
        this.isClick1 = false;
        console.log('========OnBtnStopPPClick===========',this.dwType);
        globalThis.gameMgr._GameHallMsg.ThemeMatchPPStopReq(this.dwType);
    }

    //播放正在匹配中的动画
    private PlayAni(isAni: boolean){
        this.dian.active = isAni;
    }

    //开始匹配返回
    private onmsgThemeMatchPPRes(data: ThemeMatchPPRes){
        this.isClick = true;
        console.log('==========onmsgThemeMatchPPRes===========');
        this.PlayAni(false);

        //记录主题赛的玩法类型
        globalThis.gameMgr.zts_type = this.dwType; 

        this.unschedule(this.setTime);

        if(this.hasProInObj(data,'code')){
            let code = data.code || 0;
            if(code !== Respones.OK){ //匹配成功
              
            }else{
                GameTip.inst.showTip('匹配失败！');
            }
        }
    }

    //停止匹配返回
    private onmsgThemeMatchPPStopRes(data: ThemeMatchPPStopRes){
        this.isClick1 = true;
        console.log('==========onmsgThemeMatchPPStopRes===========');

    }
}


